package com.jieao.common.utils.file.constant;

import lombok.Getter;

/**
 * @Author LWF
 * @Date2021/4/12 10:26
 **/
@Getter
public enum FileTypeEnum {
    bmp(".bmp", "image/bmp", OSSProperty.imgBucketName, OSSProperty.imgCdn),
    gif(".gif", "image/gif", OSSProperty.imgBucketName, OSSProperty.imgCdn),
    jpeg(".jpeg", "image/jpeg", OSSProperty.imgBucketName, OSSProperty.imgCdn),
    jpg(".jpg", "image/jpg", OSSProperty.imgBucketName, OSSProperty.imgCdn),
    png(".png", "image/png", OSSProperty.imgBucketName, OSSProperty.imgCdn),

    html(".html", "text/html", OSSProperty.fileBucketName, OSSProperty.fileCdn),
    txt(".txt", "text/plain", OSSProperty.fileBucketName, OSSProperty.fileCdn),
    xml(".xml", "text/xml", OSSProperty.fileBucketName, OSSProperty.fileCdn),
    vsd(".vsd", "application/vnd.visio", OSSProperty.fileBucketName, OSSProperty.fileCdn),
    ppt(".ppt", "application/vnd.ms-powerpoint", OSSProperty.fileBucketName, OSSProperty.fileCdn),
    pptx(".pptx", "application/vnd.ms-powerpoint", OSSProperty.fileBucketName, OSSProperty.fileCdn),
    doc(".doc", "application/msword", OSSProperty.fileBucketName, OSSProperty.fileCdn),
    docx(".docx", "application/msword", OSSProperty.fileBucketName, OSSProperty.fileCdn),
    zip(".zip", "application/zip", OSSProperty.fileBucketName, OSSProperty.fileCdn),
    gzip(".gzip", "application/gzip", OSSProperty.fileBucketName, OSSProperty.fileCdn),
    xls(".xls", "application/x-xls", OSSProperty.fileBucketName, OSSProperty.fileCdn),

    avi(".avi", "video/avi", OSSProperty.videoBucketName, OSSProperty.videoCdn),
    mp4(".mp4", "video/mpeg4", OSSProperty.videoBucketName, OSSProperty.videoCdn),
    wmv(".wmv", "video/x-ms-wmv", OSSProperty.videoBucketName, OSSProperty.videoCdn),
    wmx(".wmx", "video/x-ms-wmv", OSSProperty.videoBucketName, OSSProperty.videoCdn),
    rm(".rm", "application/vnd.rn-realmedia", OSSProperty.videoBucketName, OSSProperty.videoCdn),
    rmvb(".rmvb", "application/vnd.rn-realmedia-vbr", OSSProperty.videoBucketName, OSSProperty.videoCdn),

    ;
    /**
     * 文件扩展名
     */
    final String extension;
    /**
     * contentType
     */
    final String contentType;

    final String bucketName;

    final String cdnUrl;

    FileTypeEnum(final String extension, final String contentType, final String bucketName, final String cdnUrl) {
        this.extension = extension;
        this.contentType = contentType;
        this.bucketName = bucketName;
        this.cdnUrl = cdnUrl;
    }

    public static FileTypeEnum getFileTypeByExtension(String fileExtension) {
        FileTypeEnum[] values = FileTypeEnum.values();
        for (int i = 0; i < values.length; i++) {
            if (values[i].getExtension().equals(fileExtension)) {
                return values[i];
            }
        }
        return null;
    }

    public static String getContentType(String fileExtension) {
        FileTypeEnum[] values = FileTypeEnum.values();
        for (int i = 0; i < values.length; i++) {
            if (values[i].getExtension().equals(fileExtension)) {
                return values[i].getContentType();
            }
        }
        return "application/octet-stream";
    }

    public static String getFileExtension(String contentType) {
        FileTypeEnum[] values = FileTypeEnum.values();
        for (int i = 0; i < values.length; i++) {
            if (values[i].getContentType().equals(contentType)) {
                return values[i].getExtension();
            }
        }
        return "text/plain";
    }

    public static String getBucketName(String fileExtension) {
        FileTypeEnum[] values = FileTypeEnum.values();
        for (int i = 0; i < values.length; i++) {
            if (values[i].getExtension().equals(fileExtension)) {
                return values[i].getBucketName();
            }
        }
        return OSSProperty.fileBucketName;
    }

    public static String getCdnUrl(String fileExtension) {
        FileTypeEnum[] values = FileTypeEnum.values();
        for (int i = 0; i < values.length; i++) {
            if (values[i].getExtension().equals(fileExtension)) {
                return values[i].getCdnUrl();
            }
        }
        return "";
    }
}
